﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;
using TestEnvirementCL.POCO;
using TestEnvirementCL.POCO.Users;

namespace TestEnvirementCL.Concrete
{
  public class EFDbContextInitializer : DropCreateDatabaseAlways<EFDbContext>
  {
    protected override void Seed(EFDbContext context)
    {
      
      //Add Coaches
      new List<Coach>()
      {
        new Coach {Name = "Joske Vermeulen",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="joske@se.be",Gender='M',BirthDate= new DateTime(1972,4,29), PhoneNumber=32925473658},
        new Coach {Name = "Anneke Panneke",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="anneke@se2.be",Gender='F',BirthDate= new DateTime(1973,4,29), PhoneNumber=32925473658},
        new Coach {Name = "Jan Janssens",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="jan@se.be",Gender='M',BirthDate= new DateTime(1974,4,29), PhoneNumber=32925473658}
      }.ForEach(c=> context.Coaches.Add(c));
      
      //Saving the changes to the databases
      context.SaveChanges();
      
      //Add Sports
      new List<Sport>
      {
        new Sport { Name="Athletics",Abbreviation="ATH"},
        new Sport { Name="Swimming",Abbreviation="SWI"}
      }.ForEach(s=>context.Sports.Add(s));

      //Saving the changes to the databases
      context.SaveChanges();
      
      // Add Advertisements
      new List<Advertisement>
      {
        new Advertisement  { CompanyName="Coca-Cola", Logo="logo", Begin = new DateTime(2013,3,8),End= new DateTime(2013,8,8),Sport= context.Sports.Single(s => s.Abbreviation=="ATH")},
        new Advertisement  { CompanyName="Spa", Logo="logo", Begin = new DateTime(2013,4,9),End= new DateTime(2013,7,9),Sport=context.Sports.Single(s => s.Abbreviation=="SWI")},
        new Advertisement  { CompanyName="Coca-Cola", Logo="logo", Begin = new DateTime(2013,6,5),End= new DateTime(2013,12,5),Sport= context.Sports.Single(s => s.Abbreviation=="ATH")},
        new Advertisement  { CompanyName="Coca-Cola", Logo="logo", Begin = new DateTime(2013,5,10),End= new DateTime(2013,11,10),Sport= context.Sports.Single(s => s.Abbreviation=="SWI")}
      }.ForEach(a => context.Advertisements.Add(a));

      //Saving the changes to the databases
      context.SaveChanges();
      
      //Add Countries
      new List<Country>
      {
        new Country {Name="Belgium",Continent="Europe"},
        new Country {Name="Zimbabwe",Continent="Africa"},
        new Country {Name="USA",Continent="North-America"},
        new Country {Name="Brazil",Continent="South-America"},
        new Country {Name="Japan",Continent="Asia"}
      }.ForEach(c=> context.Countries.Add(c));

      //Saving the changes to the databases
      context.SaveChanges();

      //Add Regions
      new List<Region>
      {
        new Region { Name = "New York", Country = context.Countries.Single(c => c.Name == "USA")},
        new Region { Name = "Flanders",Country = context.Countries.Single(c=> c.Name == "Belgium")},
        new Region { Name = "Brazil", Country = context.Countries.Single(c=> c.Name == "Brazil")},
        new Region { Name = "Hokkaido", Country = context.Countries.Single(c=> c.Name == "Japan")}
      }.ForEach(r=> context.Regions.Add(r));
      
      //Saving the changes to the databases
      context.SaveChanges();
      
      //Add NSSFs
      new List<NSSF>
      {
        new NSSF { Name = "SVS", Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", PhoneNumber=32925473658,Fax=32925473658,Email="nssf@svs.be",DateAdded = new DateTime(2012,5,5),Region = context.Regions.Single(r=> r.Name=="New York")},
        new NSSF { Name = "SNS", Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", PhoneNumber=32925473658,Fax=32925473658,Email="nssf@sns.be",DateAdded = new DateTime(2012,5,5),Region = context.Regions.Single(r=> r.Name=="Flanders")},
        new NSSF { Name = "SBS", Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", PhoneNumber=32925473658,Fax=32925473658,Email="nssf@sbs.be",DateAdded = new DateTime(2012,5,5),Region = context.Regions.Single(r=> r.Name=="Brazil")},
        new NSSF { Name = "TKT", Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", PhoneNumber=32925473658,Fax=32925473658,Email="nssf@tkt.be",DateAdded = new DateTime(2012,5,5),Region = context.Regions.Single(r=> r.Name=="Hokkaido")}
      }.ForEach(n=> context.NSSFs.Add(n));

      //Saving the changes to the databases
      context.SaveChanges();

      new List<NSSFRep>
      {
        new NSSFRep {Name = "Peter Pan",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="peter@svs.be",Gender='M',BirthDate= new DateTime(1972,4,29), PhoneNumber=32925473658,NSSF = context.NSSFs.Single(n=> n.Name=="SVS")},
        new NSSFRep {Name = "Snow White",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="snow@sns.be",Gender='F',BirthDate= new DateTime(1973,4,29), PhoneNumber=32925473658,NSSF = context.NSSFs.Single(n=> n.Name=="SNS")},
        new NSSFRep {Name = "Klein Duimpje",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="klein@sbs.be",Gender='M',BirthDate= new DateTime(1972,4,29), PhoneNumber=32925473658,NSSF = context.NSSFs.Single(n=> n.Name=="SBS")},
        new NSSFRep {Name = "Bruce Lee",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="bruce@tkt.be",Gender='F',BirthDate= new DateTime(1973,4,29), PhoneNumber=32925473658,NSSF = context.NSSFs.Single(n=> n.Name=="TKT")}
      
      }.ForEach(n => context.NSSFReps.Add(n));
      
      //Add Events
      new List<Event>
      {
        new Event { Name="short-jumping",Type=false,Measure="Distance",Sport = context.Sports.Single(s => s.Abbreviation == "ATH")},
        new Event { Name="100m baksteenslag",Type=false,Measure="Time",Sport = context.Sports.Single(s => s.Abbreviation == "SWI")},
        new Event { Name="100m fly",Type=true,Measure="Time",Sport = context.Sports.Single(s => s.Abbreviation == "SWI")},
        new Event { Name="100m sprint",Type=true,Measure="Time",Sport = context.Sports.Single(s => s.Abbreviation == "ATH")},
      }.ForEach(e=> context.Events.Add(e));

      //Saving the changes to the databases
      context.SaveChanges();
            
      //Add Schools
      new List<School>
      {
        new School { Name = "Test 1",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="se@test1.be",Abbreviation="T1",NSSF=context.NSSFs.Single(n=> n.Name=="SVS")},
        new School { Name = "Test 2",Street = "Zottendijk 6", City="Antwerpen", Zipcode="BE-5586", Email="se@test2.be",Abbreviation="T2",NSSF=context.NSSFs.Single(n=> n.Name=="SNS")},
        new School { Name = "Test 3",Street = "Zottendijk 7", City="Antwerpen", Zipcode="BE-5586", Email="se@test3.be",Abbreviation="T3",NSSF=context.NSSFs.Single(n=> n.Name=="SBS")},
        new School { Name = "Test 4",Street = "Zottendijk 8", City="Antwerpen", Zipcode="BE-5586", Email="se@test4.be",Abbreviation="T4",NSSF=context.NSSFs.Single(n=> n.Name=="TKT")}
      }.ForEach(s=> context.Schools.Add(s));

      //Saving the changes to the databases
      context.SaveChanges();
      
      //Add Teams
      new List<Team>
      {
        new Team { Name="De snelle eddies",Photo="photo",Logo="logo",Gender='M',Coach=context.Coaches.Single(c=> c.Name=="Joske Vermeulen"),School= context.Schools.Single(s=> s.Abbreviation=="T1"),Sport = context.Sports.Single(s=> s.Abbreviation=="ATH")},
        new Team { Name="Streamliners",Photo="photo",Logo="logo",Gender='F',Coach=context.Coaches.Single(c=> c.Name=="Anneke Panneke"),School= context.Schools.Single(s=> s.Abbreviation=="T2"),Sport = context.Sports.Single(s=> s.Abbreviation=="SWI")},
        new Team { Name="Prototypes",Photo="photo",Logo="logo",Gender='M',Coach=context.Coaches.Single(c=> c.Name=="Jan Janssens"),School= context.Schools.Single(s=> s.Abbreviation=="T3"),Sport = context.Sports.Single(s=> s.Abbreviation=="ATH")}
      }.ForEach(t => context.Teams.Add(t));

      //Saving the changes to the databases
      context.SaveChanges();

      //Add Meetings
      new List<Meeting>
      {
        new Meeting {Name="World Champ",Location="Lille",Type=true,Date= new DateTime(2013,3,21),NSSF = context.NSSFs.Single(n=> n.Name=="SVS")},
        new Meeting {Name="World Champ 12",Location="Rio",Type=true,Date= new DateTime(2012,3,21),NSSF = context.NSSFs.Single(n=> n.Name=="SVS")},
        new Meeting {Name="Region Champ",Location="Barcelona",Type=true,Date= new DateTime(2013,4,27),NSSF = context.NSSFs.Single(n=> n.Name=="SNS")},
        new Meeting {Name="Training",Location="Antwerp",Type=false,Date= new DateTime(2013,4,5),Coach = context.Coaches.Single(n=> n.Name=="Joske Vermeulen")}
        
      }.ForEach(m => context.Meetings.Add(m));

      //Saving the changes to the databases
      context.SaveChanges();

      //Adding Meetings to Teams and Teams to Meetiggs
      foreach (var team in context.Teams)
      {
        foreach (var meeting in context.Meetings)
        {


          team.Meetings = context.Meetings.ToList<Meeting>();

          meeting.Teams = context.Teams.ToList<Team>();
        }

      }

      //Saving the changes to the databases
      context.SaveChanges();

       //Add Records
      new List<Record>
      { 
          new Record { RecordHolder = "Usain Bolt", Result = 9.58 , Event = context.Events.Single(e=> e.Name == "100m sprint"), Meeting = context.Meetings.Single(m => m.Name=="World Champ")},
          new Record { RecordHolder = "Michael Phelps", Result = 49.82, Event = context.Events.Single(e=> e.Name == "100m fly"),Meeting = context.Meetings.Single(m => m.Name=="World Champ")}
      }.ForEach(r => context.Records.Add(r));

      //Saving the changes to the databases
      context.SaveChanges();
      

      //Add ISFAdmins
      new List<ISFAdmin>
      {
        new ISFAdmin {Name = "Pieter Pauwels",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="pieter@isf.be",Gender='M',BirthDate= new DateTime(1972,4,29), PhoneNumber=32925473658},
      }.ForEach(i=> context.ISFAdmins.Add(i));

      //Saving the changes to the databases
      context.SaveChanges();
      
      //Add NSSFRep
     

      //Saving the changes to the databases
      context.SaveChanges();

      //Add SCCs
      
      new List<SCC>
      {
        new SCC {Name = "S1 Test",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s1@se.be",Gender='M',BirthDate= new DateTime(1972,4,29), PhoneNumber=32925473658,School = context.Schools.Single(s=> s.Name=="Test 1")},
        new SCC {Name = "S2 Test",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s2@se.be",Gender='F',BirthDate= new DateTime(1973,4,29), PhoneNumber=32925473658,School = context.Schools.Single(s=> s.Name=="Test 2")},
        new SCC {Name = "S3 Test",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s3@se.be",Gender='M',BirthDate= new DateTime(1972,4,29), PhoneNumber=32925473658,School = context.Schools.Single(s=> s.Name=="Test 3")},
        new SCC {Name = "S4 Test",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s4@se.be",Gender='F',BirthDate= new DateTime(1973,4,29), PhoneNumber=32925473658,School = context.Schools.Single(s=> s.Name=="Test 4")}
      
      }.ForEach(s => context.SCCs.Add(s));

      //Saving the changes to the databases
      context.SaveChanges();
      
      //Add Students
      new List<Student>
      {
        new Student {Name = "S1",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s1@se.be",Gender='M',BirthDate= new DateTime(1995,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S2",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s2@se.be",Gender='F',BirthDate= new DateTime(1996,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S3",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s3@se.be",Gender='M',BirthDate= new DateTime(1997,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S4",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s4@se.be",Gender='F',BirthDate= new DateTime(1998,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S5",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s1@se.be",Gender='M',BirthDate= new DateTime(1995,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S6",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s2@se.be",Gender='F',BirthDate= new DateTime(1996,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S7",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s3@se.be",Gender='M',BirthDate= new DateTime(1997,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S8",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s4@se.be",Gender='F',BirthDate= new DateTime(1998,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S9",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s1@se.be",Gender='M',BirthDate= new DateTime(1995,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S10",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s2@se.be",Gender='F',BirthDate= new DateTime(1996,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S11",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s3@se.be",Gender='M',BirthDate= new DateTime(1997,4,29), PhoneNumber=32925473658,Photo="photo"},
        new Student {Name = "S12",Street = "Zottendijk 5", City="Antwerpen", Zipcode="BE-5586", Email="s4@se.be",Gender='F',BirthDate= new DateTime(1998,4,29), PhoneNumber=32925473658,Photo="photo"}
      
      
      }.ForEach(s=> context.Students.Add(s));
     
      //Saving the changes to the databases
      context.SaveChanges();
      
      //Adding Students to Teams and Teams to Students
      foreach (var team in context.Teams)
      {
        foreach (var student in context.Students) 
        {
          

          team.Students = context.Students.ToList<Student>();

          student.Teams = context.Teams.ToList<Team>();
        }
        
      }

      //Saving the changes to the databases
      context.SaveChanges();
       
    }
  }
}
